package com.fdb.efp.nls.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.fdb.basic.framework.core.base.FrameworkService;
import com.fdb.efp.nls.service.dao.LoanUseRecordDao;
import com.fdb.efp.nls.service.domain.LoanUseRecord;
import com.fdb.efp.nls.service.facade.LoanUseRecordService;
import com.fdb.efp.nls.service.vo.LoanUseRecordVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 放款用途记录表的Service实现类
 * </p>
 * @author yusys
 * @createDate 2018-06-25 15:58:01
 */
@Service("loanUseRecordService")
public class LoanUseRecordServiceImpl implements LoanUseRecordService, FrameworkService {
    
    private static Logger logger = LoggerFactory.getLogger(LoanUseRecordServiceImpl.class);
    
    @Autowired
    private LoanUseRecordDao loanUseRecordDao;
    
    @Override
    public int insertLoanUseRecord(LoanUseRecordVO inLoanUseRecordVo) {
        logger.debug("当前新增数据为：" + inLoanUseRecordVo.toString());
        int count = 0;
        try {
            LoanUseRecord loanUseRecord = new LoanUseRecord();
            beanCopy(inLoanUseRecordVo, loanUseRecord);
            count = loanUseRecordDao.insertLoanUseRecord(loanUseRecord);
        } catch (Exception e) {
            logger.error("新增数据发生异常！", e);
            count = -1;
        }
        logger.debug("当前新增数据条数为：" + count);
        return count;
    }
    
    @Override
    public int deleteByPk(LoanUseRecordVO inLoanUseRecordVo) {
        logger.debug("当前删除数据条件为：" + inLoanUseRecordVo.toString());
        int count = 0;
        try {
            LoanUseRecord loanUseRecord = new LoanUseRecord();
            beanCopy(inLoanUseRecordVo, loanUseRecord);
            count = loanUseRecordDao.deleteByPk(loanUseRecord);
        } catch (Exception e) {
            logger.error("删除数据发生异常！", e);
            count = -1;
        }
        logger.debug("根据条件：" + inLoanUseRecordVo + "删除的数据条数为" + count);
        return count;
    }
    
    @Override
    public int updateByPk(LoanUseRecordVO inLoanUseRecordVo) {
        logger.debug("当前修改数据为：" + inLoanUseRecordVo.toString());
        int count = 0;
        try {
            LoanUseRecord loanUseRecord = new LoanUseRecord();
            beanCopy(inLoanUseRecordVo, loanUseRecord);
            count = loanUseRecordDao.updateByPk(loanUseRecord);
        } catch (Exception e) {
            logger.error("修改数据发生异常！", e);
            count = -1;
        }
        logger.debug("根据条件：" + inLoanUseRecordVo + "修改的数据条数为" + count);
        return count;
    }
    
    @Override
    public LoanUseRecordVO queryByPk(LoanUseRecordVO inLoanUseRecordVo) {
        logger.debug("当前查询参数信息为：" + inLoanUseRecordVo);
        try {
            LoanUseRecord loanUseRecord = new LoanUseRecord();
            beanCopy(inLoanUseRecordVo, loanUseRecord);
            LoanUseRecord queryloanUseRecord = loanUseRecordDao.queryByPk(loanUseRecord);
            if (Objects.nonNull(queryloanUseRecord)) {
                LoanUseRecordVO outLoanUseRecordVo = beanCopy(queryloanUseRecord, new LoanUseRecordVO());
                logger.debug("当前查询结果为：" + outLoanUseRecordVo.toString());
                return outLoanUseRecordVo;
            } else {
                logger.debug("当前查询结果为空！");
            }
        } catch (Exception e) {
            logger.error("查询数据发生异常！", e);
        }
        return null;
    }
    /**
     * 根据查询条件返回记录
     * @param loanUseRecordVo
     * @return
     */
	@Override
	public List<LoanUseRecordVO> queryByCondition(LoanUseRecordVO loanUseRecordVo) {
		logger.info("传入的查询条件为:"+JSONObject.toJSONString(loanUseRecordVo));
		List<LoanUseRecordVO> list = null;
		LoanUseRecord loanUseRecord = beanCopy(loanUseRecordVo, new LoanUseRecord());
		try {
			List<LoanUseRecord> loanUseList = loanUseRecordDao.queryByCondition(loanUseRecord);
			logger.info("查询结果集为:"+JSONObject.toJSONString(loanUseList));
			list = (List<LoanUseRecordVO>) beansCopy(loanUseList, LoanUseRecordVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
			return null;
		}
		return list;
	}
}